package cn.highedu.spring03.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理类
 */
@Component
@RestControllerAdvice
public class ExceptionAdvice {
    Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
    @ExceptionHandler(UserNotFoundException.class) // 异常类型
    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    public Map<String, Object> handleUserNotFoundException(UserNotFoundException e) {
        logger.info("用户信息未找到", e);
        Map<String, Object> result = new HashMap<>();
        result.put("message", e.getMessage());
        return result;
    }
}
